//
//  Swizzle.swift
//  MYModule
//
//  Created by 李然 on 2022/8/2.
//

import Foundation
import UIKit
import ObjectiveC

fileprivate struct AssociatedKeys{
    fileprivate static var widgetIdKey: Bool = false
    fileprivate static var pageNameKey: Bool = false
}

private var TableViewCellSwizzed = false
private var CollectionViewCellSwizzed = false
private var ViewControllerSwizzed = false


extension UIView {
    
    public var widgetId:String{
        get {
            objc_getAssociatedObject(self, &AssociatedKeys.widgetIdKey) as? String ?? ""
        }
        set {
            objc_setAssociatedObject(self, &AssociatedKeys.widgetIdKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }
}

extension UITableViewCell{
    
    public static func setupSwizzling(){
        guard !TableViewCellSwizzed else {return}
        TableViewCellSwizzed = true
        
        if self !== UITableViewCell.self { return }

        let originalSelector = #selector(layoutSubviews)
        let swizzledSelector = #selector(df_layoutSubviews)

        let originalMethod = class_getInstanceMethod(UITableViewCell.self, originalSelector)
        let swizzledMethod = class_getInstanceMethod(UITableViewCell.self, swizzledSelector)
        method_exchangeImplementations(originalMethod!, swizzledMethod!)
    }
    
    @objc private func df_layoutSubviews(){
        df_layoutSubviews()
        if let pageName = self.viewController?.pageName,!pageName.isEmpty{
            ViewAttributeTool.apply(self.contentView, pageName)
        }
    }
}

extension UICollectionViewCell{
    
    public static func setupSwizzling(){
        guard !CollectionViewCellSwizzed else {return}
        CollectionViewCellSwizzed = true
        
        if self !== UICollectionViewCell.self { return }

        let originalSelector = #selector(layoutSubviews)
        let swizzledSelector = #selector(df_layoutSubviews)

        let originalMethod = class_getInstanceMethod(UICollectionViewCell.self, originalSelector)
        let swizzledMethod = class_getInstanceMethod(UICollectionViewCell.self, swizzledSelector)
        method_exchangeImplementations(originalMethod!, swizzledMethod!)
    }
    
    @objc private func df_layoutSubviews(){
        df_layoutSubviews()
        if let pageName = self.viewController?.pageName,!pageName.isEmpty{
            ViewAttributeTool.apply(self.contentView, pageName)
        }
    }
}

extension UIViewController {
    
    public var pageName:String{
        get {
            objc_getAssociatedObject(self, &AssociatedKeys.pageNameKey) as? String ?? ""
        }
        set {
            objc_setAssociatedObject(self, &AssociatedKeys.pageNameKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }
    
    public static func setupSwizzling() {
        
        guard !ViewControllerSwizzed else {return}
        ViewControllerSwizzed = true

        if self !== UIViewController.self { return }

        let originalSelector = #selector(viewWillLayoutSubviews)
        let swizzledSelector = #selector(df_viewWillLayoutSubviews)

        let originalMethod = class_getInstanceMethod(UIViewController.self, originalSelector)
        let swizzledMethod = class_getInstanceMethod(UIViewController.self, swizzledSelector)
        method_exchangeImplementations(originalMethod!, swizzledMethod!)
    }
    
    @objc private func df_viewWillLayoutSubviews() {
        df_viewWillLayoutSubviews()
        guard !self.pageName.isEmpty else {
            return
        }
        ViewAttributeTool.apply(self.view, self.pageName)
    }
}


fileprivate struct ViewAttributeTool{
    
    static func apply(_ view:UIView, _ pageName:String){
        self.applyAttr(view, pageName)
        guard view.subviews.count > 0 else {
            return
        }
        for subView in view.subviews{
            apply(subView, pageName)
        }
    }
    
    static func applyAttr(_ view:UIView, _ pageName:String){
        guard !view.widgetId.isEmpty else {
            return
        }
        guard let attr = ViewConfigManager.shared.getViewAttribete(by: pageName, widgetId: view.widgetId) else {
            return
        }
        if let bgColor = attr.backgroundColor{
            view.backgroundColor = bgColor
        }
        if let cornerSize = attr.corner_size{
            view.layer.cornerRadius = cornerSize
        }
        if let show = attr.is_show_shadow,show == 0{
            view.layer.shadowRadius = 0
            view.layer.shadowColor = UIColor.clear.cgColor
            view.layer.shadowOffset = CGSize.zero
        }
        
        if let label = view as? UILabel{
            if let font_size = attr.text_size{
                label.font = Fonts.systemFont(ofSize: font_size)
            }
            if let textColor = attr.textColor{
                label.textColor = textColor
            }
        }
        //TODO: - 其他view的自定义
    }
}
